#include "Packet.hpp"
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;

namespace wd
{
Packet::Packet()
    :_type(0)
     ,_length(0)
     ,_msg()
    {}
Packet::Packet(int type,string &msg)
    :_type(type)
     ,_length()
     ,_msg(msg)
    {
        _length=msg.length();
    }
Packet::~Packet()
{}
//转换为TLV
TLV Packet::toTLV() const
{
    TLV tlv;
    tlv._type=this->_type;
    tlv._length=this->_length;
    if(_length>0&&_length<1024)
    {
       memcpy(tlv.data,this->_msg.c_str(),_length);
    }
    return tlv;
}
//从TLV转换解析网络数据
void Packet::fromTLV(const TLV &tlv)
{
   this->_type=tlv._type;
   this->_length=tlv._length;
   //char数组转string指定长度
   this->_msg.assign(tlv.data,tlv._length);
}
}


